<template>
  <div class="app-container">
    <!-- 头部 Button -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleSearch">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-check"
          size="mini"
          :disabled="single"
          @click="check_submit = true"
          v-hasPermi="['device:equipmentRepairAccept:accept']">
          验收
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="check_remove = true"
          v-hasPermi="['device:equipmentRepairAccept:back']">
          撤回
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['device:equipmentRepair:export']">
          导出
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#0323af">审核</Tag>
      <Tag type="dot" size="default" color="#bb0331">验收</Tag>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 主表数据 -->
    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :cell-style="iCellStyle"
      :data="WxmMstrList"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center' }"
      :height="tableH">
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column label="维修单号" :show-overflow-tooltip="true" width="100" align="center" prop="wxmDjbh" />
      <el-table-column label="设备代码" :show-overflow-tooltip="true" width="100" align="center" prop="wxmSbdm" />
      <el-table-column label="设备名称" :show-overflow-tooltip="true" width="100" align="center" prop="wxmSbName" />
      <el-table-column label="申请部门" :show-overflow-tooltip="true" width="100" align="center" prop="wxmBmdm">
        <template v-slot="scope">
          <dict-tag :options="deptList" :value="scope.row.wxmBmdm" />
        </template>
      </el-table-column>
      <el-table-column label="申请人员" :show-overflow-tooltip="true" width="100" align="center" prop="wxmSqry" />
      <el-table-column label="停机时间" :show-overflow-tooltip="true" align="center" prop="wxmKssj" width="180" />
      <el-table-column label="维修时间" :show-overflow-tooltip="true" align="center" prop="wxmWxsj" width="180" />
      <el-table-column label="维修工时" :show-overflow-tooltip="true" width="100" align="center" prop="wxmWxgs" />
      <el-table-column label="维修人员" :show-overflow-tooltip="true" width="100" align="center" prop="wxmWxry" />
      <el-table-column label="撤回原因" :show-overflow-tooltip="true" width="100" align="center" prop="wxmNgdesc" />
      <el-table-column label="建立人员" :show-overflow-tooltip="true" width="100" align="center" prop="wxmJlry" />
      <el-table-column label="建立日期" :show-overflow-tooltip="true" align="center" prop="wxmJlrq" width="180" />
      <el-table-column label="分析人员" :show-overflow-tooltip="true" width="100" align="center" prop="wxmFxry" />
      <el-table-column label="分析日期" :show-overflow-tooltip="true" align="center" prop="wxmFxrq" width="180" />
      <el-table-column label="验收人员" :show-overflow-tooltip="true" width="100" align="center" prop="wxmPzry" />
      <el-table-column label="验收日期" :show-overflow-tooltip="true" align="center" prop="wxmPzrq" width="180" />
      <el-table-column label="总停机时间（小时）" :show-overflow-tooltip="true" align="center" prop="wxmZjsj" width="140" />
    </el-table>

    <!--子表Tab-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="申请及维修内容" name="first">
        <el-form ref="form" :model="wxmMstr" label-width="100px">
          <el-col :span="6">
            <el-form-item label="维修单号" prop="wxmDjbh">
              <el-input v-model="wxmMstr.wxmDjbh" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="申请人员" prop="wxmSqry">
              <el-input v-model="wxmMstr.wxmSqry" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="申请部门" prop="wxmBmdm">
              <el-input v-model="wxmMstr.wxmBmdm" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="设备代码" prop="wxmSbdm">
              <el-input v-model="wxmMstr.wxmSbdm" readonly />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="设备名称" prop="wxmSbName">
              <el-input v-model="wxmMstr.wxmSbName" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="维修人员" prop="wxmWxry">
              <el-input v-model="wxmMstr.wxmWxry" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="维修工时" prop="wxmWxgs">
              <el-input v-model="wxmMstr.wxmWxgs" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="建立人员" prop="wxmJlry">
              <el-input v-model="wxmMstr.wxmJlry" readonly />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="分析人员" prop="wxmFxry">
              <el-input v-model="wxmMstr.wxmFxry" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="停机时间" prop="wxmKssj">
              <el-input v-model="wxmMstr.wxmKssj" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="维修时间" prop="wxmWxsj">
              <el-input v-model="wxmMstr.wxmWxsj" readonly />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="故障原因" prop="wxmGzms">
              <el-input type="textarea" v-model="wxmMstr.wxmGzms" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原因处理" prop="wxmClms">
              <el-input type="textarea" v-model="wxmMstr.wxmClms" readonly />
            </el-form-item>
          </el-col>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="更换零件清单" name="second">
        <el-table border stripe :data="wxdDetList" :height="tableHChild" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column label="部件名称" align="center" prop="wxdPmgg" />
          <el-table-column label="数量" align="center" prop="wxdSl" />
          <el-table-column label="单价(元)" align="center" prop="wxdCgdj" />
          <el-table-column label="金额(元)" align="center" prop="wxdCgje" />
          <el-table-column label="备注" align="center" prop="wxdDesc" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="部门验收" name="third">
        <el-form ref="form" :model="wxmMstr" label-width="100px">
          <el-col :span="12">
            <el-form-item label="验收人员" prop="wxmPzry">
              <el-input v-model="wxmMstr.wxmPzry" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门验收" prop="wxmYsms">
              <el-input type="textarea" v-model="wxmMstr.wxmYsms" readonly />
            </el-form-item>
          </el-col>
        </el-form>
      </el-tab-pane>
    </el-tabs>

    <!-- 撤回 -->
    <Modal v-model="check_remove" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认撤回？</span>
      </p>
      <div style="text-align: center">
        <p>确认将此数据撤回重写吗</p>
        <Input style="width: 90%" v-model="backReason" placeholder="撤回原因" />
      </div>
      <div slot="footer">
        <Button type="error" size="large" long @click="handleBack">撤回</Button>
      </div>
    </Modal>
    <!-- 验收 -->
    <Modal v-model="check_submit" draggable scrollable width="300">
      <p slot="header" style="text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认</span>
      </p>
      <div style="text-align: center">
        <p>确认将此数据验收吗？</p>
        <p>验收后数据锁定无法撤回，请谨慎操作！</p>
        <Input style="width: 90%" v-model="suggestion" placeholder="验收意见" />
      </div>
      <div slot="footer">
        <Button type="success" size="large" long @click="accept">验收</Button>
      </div>
    </Modal>

    <!-- 查询对话框 -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="420px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="维修单号" prop="wxmDjbh">
            <el-input
              class="input240"
              v-model="queryParams.wxmDjbh"
              placeholder="请输入维修单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备代码" prop="wxmSbdm">
            <el-input
              class="input240"
              v-model="queryParams.wxmSbdm"
              placeholder="请输入设备名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备名称" prop="wxmSbName">
            <el-input
              class="input240"
              v-model="queryParams.wxmSbName"
              placeholder="请输入设备名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-form-item prop="wxmZtbz">
            <el-radio-group v-model="queryParams.wxmZtbz">
              <el-radio label="2">未验收</el-radio>
              <el-radio label="4">已验收</el-radio>
              <el-radio label="">全部</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipmentRepair, getEquipmentRepair, getWxdDet, acceptWxm, backAccpetWxm, exportModelRepair } from '@/api/device/equipmentRepair'
import { listDept } from '@/api/system/dept'
import { userDict } from '@/api/system/user'

export default {
  name: 'EquipmentRepairAccept',
  data() {
    return {
      // 遮罩层
      loading: true,
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      //显示的表格
      activeTab: 'first',
      // 选中数组
      rowData: null,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 设备维修单表格数据 -WxmMstr
      WxmMstrList: [],
      // 设备维修单表格数据 -WxdDet
      wxdDetList: [],
      // 全部部门
      deptList: [],
      //用户
      UserOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //验收弹出层
      check_submit: false,
      // 撤回弹出层
      check_remove: false,
      //验收意见
      suggestion: null,
      //撤回
      backReason: null,
      // 建立日期时间范围
      dateRange: [new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 60), new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 60)],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        wxmDjbh: null,
        wxmSbName: null,
        wxmJlrq: null,
        wxmZtbz: '2',
        wxmTjflag: '1',
      },
      // 表单参数
      form: {},
      wxmMstr: {},
    }
  },
  created() {
    //用户字典
    userDict().then((res) => {
      this.UserOptions = res.data
    })
    // 获取全部部门
    listDept().then((res) => {
      this.deptList = res.data
      for (let item of this.deptList) {
        item.dictLabel = item.deptName
        item.listClass = 'default'
        item.dictValue = item.deptId + ''
      }
    })
    this.getList()
  },
  methods: {
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.wxmZtbz == '1' && row.wxmTjflag == '1') {
        return 'padding:0px 0;color:#0fac09'
      }
      //审核
      else if (row.wxmZtbz == '2' && row.wxmTjflag == '1') {
        return 'padding:0px 0;color:#0323af'
      }
      //验收
      else if (row.wxmZtbz == '4' && row.wxmTjflag == '1') {
        return 'padding:0px 0;color:#bb0331'
      }
    },
    /** 查询设备维修单列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.params['beginWxmJlrq'] = this.moment(this.dateRange[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.params['endWxmJlrq'] = this.moment(this.dateRange[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      this.queryParams.pageNum = 1
      listEquipmentRepair(this.queryParams).then((response) => {
        this.WxmMstrList = []
        this.WxmMstrList = this.WxmMstrList.concat(response.rows)
        this.total = response.total
        this.loading = false
        this.wxdDetList = []
        this.wxmMstr = {}
        if (this.rowData == null) {
          this.clickRow(this.WxmMstrList[0])
        }
      })
    },
    // rowClick 点击获取选中的头表和关联的明细
    getWxmDet(wxmId, wxmDjbh) {
      getEquipmentRepair(wxmId).then((response) => {
        this.wxmMstr = response.data
      })
      this.getWxd(wxmDjbh)
    },
    //根据 订单编号 获取明细数据
    getWxd(wxmDjbh) {
      getWxdDet(wxmDjbh).then((res) => {
        this.wxdDetList = res.data
      })
    },

    //load
    load() {
      if (this.queryParams.pageNum < Math.ceil(this.total / this.queryParams.pageSize)) {
        this.queryParams.pageNum++
        listEquipmentRepair(this.queryParams).then((response) => {
          this.WxmMstrList = this.WxmMstrList.concat(response.rows)
          this.total = response.total
          this.loading = false
        })
      }
      return 0
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        wxmId: null,
        wxmDjbh: null,
        wxmSbdm: null,
        wxmSbName: null,
        wxmBmdm: null,
        wxmSqry: null,
        wxmKssj: new Date(),
        wxmGzms: null,
        wxmClms: null,
        wxmYsms: null,
        wxmWxry: null,
        wxmWxgs: null,
        wxmWxsj: null,
        wxmTjflag: null,
        wxmZtbz: null,
        wxmNgdesc: null,
        wxmJlry: null,
        wxmJlrq: null,
        wxmFxry: null,
        wxmFxrq: null,
        wxmPzry: null,
        wxmPzrq: null,
      }
      this.resetForm('form')
    },
    //搜索
    handleSearch() {
      this.reset()
      this.title = '查询设备维修单'
      this.openSearch = true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.WxmMstrList = []
      this.getList()
      this.openSearch = false
    },
    // 多选框选中数据
    clickRow(row) {
      if (row != null) {
        this.rowData = row
        this.getWxmDet(row.wxmId, row.wxmDjbh)
      }
      this.single = row == null || row.wxmZtbz != '2' || row.wxmTjflag != '1'
    },
    /** 验收 */
    accept() {
      const wxmId = this.rowData.wxmId
      acceptWxm(wxmId, this.suggestion)
        .then((res) => {
          this.getList()
          this.check_submit = false
          this.suggestion = null
          this.msgSuccess('验收成功')
        })
        .catch(() => {
          this.msgError('验收失败')
        })
    },
    /** 撤回按钮操作 */
    handleBack() {
      const wxmId = this.rowData.wxmId
      backAccpetWxm(wxmId, this.backReason)
        .then((res) => {
          this.getList()
          this.check_remove = false
          this.backReason = null
          this.msgSuccess('撤回成功')
        })
        .catch(() => {
          this.msgError('撤回失败')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有设备维修作业数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportModelRepair(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>

<style lang="scss" scoped></style>
